package com.nmgydy.util;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MVCBuilder {

    private static String jdbc_driver = "com.mysql.jdbc.Driver";
    private static String db_servername = "localhost";
    private static String db_username = "root";
    private static String db_password = "";
    private static String db_name = "zzb_data_platform";

    //private static String table_name = "ac_live_vedio";
    private static String table_name = "zz_organization";
    private static String class_name = "Organization";
    private static String base_package_name = "com.nmgydy";
    private static String secound_level_name = "admin";
    private static String module_name = "组织架构";

    private static String template_path = "D:/file/mvc_builder/template/";
    private static String output_path = "D:/file/mvc_builder/output/"+table_name+"/";

    public static void main(String[] args) {
        List<Map<String,String>> fieldList = getSchema();
        //输出目录
        System.out.println(output_path);
        File outputDir = new File(output_path);
        if(outputDir.exists()){
            outputDir.delete();
        }
        outputDir.mkdir();
        buildFile(fieldList,"Controller.java",class_name + "Controller.java");
        buildFile(fieldList,"Model.java",class_name+".java");
        buildFile(fieldList,"list.jsp","list.jsp");
        buildFile(fieldList,"add.jsp","add.jsp");
        buildFile(fieldList,"edit.jsp","edit.jsp");
        buildFile(fieldList,"search.jsp","search.jsp");
    }

    private static void buildFile(List<Map<String,String>> fieldList,String srcFile,String targetFile){
        String pathName = class_name.substring(0, 1).toLowerCase() + class_name.substring(1);
        String secound_path_name = "";
        String secound_package_name = "";
        if(!"".equals(secound_level_name)){
            secound_path_name = "/"+secound_level_name;
            secound_package_name = "."+secound_level_name;
        }
        int len = fieldList.size();
        int dialog_height = (len / 2 + len % 2) * 40 + 92;
        if(dialog_height < 280){
            dialog_height = 280;
        }
        if(dialog_height > 600){
            dialog_height = 600;
        }
        Configuration configuration = new Configuration();
        try {
            configuration.setDirectoryForTemplateLoading(new File(template_path));
            Template template = configuration.getTemplate(srcFile);
            File docFile = new File(output_path + targetFile);
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("base_package_name",base_package_name);
            dataMap.put("secound_path_name",secound_path_name);
            dataMap.put("secound_package_name",secound_package_name);
            dataMap.put("table_name",table_name);
            dataMap.put("pathName",pathName);
            dataMap.put("module_name",module_name);
            dataMap.put("class_name",class_name);
            dataMap.put("fieldList",fieldList);
            dataMap.put("dialog_height",dialog_height);
            template.process(dataMap,out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //数据库字段设计
    private static List<Map<String,String>> getSchema(){
        String db_url = "jdbc:mysql://"+db_servername+":3306/"+db_name+"?useUnicode=true&characterEncoding=utf8";
        List<Map<String,String>> list = new ArrayList<>();
        try {
            Class.forName(jdbc_driver);
            Connection conn = DriverManager.getConnection(db_url,db_username,db_password);
            String sql = "select COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_COMMENT from information_schema.COLUMNS where TABLE_NAME = ? and COLUMN_NAME != 'id'";
            System.out.println(sql);
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, table_name);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                Map<String,String> schemaMap = new HashMap<>();
                schemaMap.put("columnName",rs.getString("COLUMN_NAME"));
                String columnName = rs.getString("COLUMN_NAME");
                schemaMap.put("columnNameBig",columnName.substring(0, 1).toUpperCase() + columnName.substring(1));
                schemaMap.put("dataType",rs.getString("DATA_TYPE"));
                schemaMap.put("isNullable",rs.getString("IS_NULLABLE"));
                schemaMap.put("columnComment",rs.getString("COLUMN_COMMENT"));
                list.add(schemaMap);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //System.out.println(JSON.toJSONString(list,true));
        return list;
    }

}
